/* Copyright (c) 2009, Antonie Jovanoski
 *	
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contact e-mail: Antonie Jovanoski <minimoog77@gmail.com>
 */

#include <QtDebug>
#include <QMessageBox>
#include "qtwitstatus.h"
#include "qtwitdget.h"

QTwitdget::QTwitdget(QWidget *parent)
	:	QWidget(parent)
{
	ui.setupUi(this);
	
	connect(ui.backButton, SIGNAL(clicked()), SLOT(backButtonClicked()));
	connect(ui.forwardButton, SIGNAL(clicked()), SLOT(forwardButtonClicked()));
	
	m_scrollAreaContents = new QWidget();
	m_verticalLayout = new QVBoxLayout(m_scrollAreaContents);
	ui.scrollArea->setWidget(m_scrollAreaContents);

	ui.backButton->setEnabled(false);
	ui.forwardButton->setEnabled(false);

	m_statusWidgetsPerPage = 20; //should be programmable
	m_currentPage = 0;

	//set StatusWidgets
	setupStatusWidgets();
}

void QTwitdget::setImageDownloader(ImageDownloader *imgDown)
{	
	m_imageDownloader = imgDown;
	connect(m_imageDownloader, SIGNAL(finished()), SLOT(finishedDownloadImages()));
}

void QTwitdget::setStatuses(const QList<QTwitStatus>& statuses)
{
	m_statuses = statuses;

	if(m_statuses.count() > m_statusWidgetsPerPage)
		ui.forwardButton->setEnabled(true);
}

void QTwitdget::setUsername(const QString& username)
{
	m_username = username;
}

void QTwitdget::setupStatusWidgets()
{
	for(int i = 0; i < m_statusWidgetsPerPage; ++i){
		StatusWidget *sw = new StatusWidget(m_scrollAreaContents);
		m_verticalLayout->addWidget(sw);
		m_statusWidgets.append(sw);
		connect(sw, SIGNAL(replyRequest(const QString&, int)), SIGNAL(requestReplyStatus(const QString&, int)));
		connect(sw, SIGNAL(deleteRequest(int)), SIGNAL(requestDeleteStatus(int)));
	}

	m_scrollAreaContents->adjustSize();
}

void QTwitdget::updateStatusWidgets()
{
	//build url image list for downloading for current page
	QList<QUrl> urlsImages;
	
	int ns = 0;
	QList<QTwitStatus>::const_iterator i = m_statuses.begin() + m_statusWidgetsPerPage * m_currentPage;
	while(i != m_statuses.end() && ns < m_statusWidgetsPerPage){

		QTwitStatus status = *i;

		urlsImages << status.profileImageUrl();

		//set statuses
		StatusWidget *sw = m_statusWidgets.at(ns);
		sw->setText(status.text());
		sw->setScreenName(status.screenName());
		sw->setSource(status.source());
		sw->setCreated(status.created());
		sw->setStatusId(status.id());

		if(status.screenName() == m_username){
			sw->setUserOwnStatus(true);
		} else {
			sw->setUserOwnStatus(false);
		}

		++i;
		++ns;
	}

	if(ns < m_statusWidgetsPerPage){
		for(int j = ns + 1; j < m_statusWidgetsPerPage; ++j){
			StatusWidget *sw = m_statusWidgets.at(j);
			sw->setText(QString());
			sw->setScreenName(QString());
			sw->setSource(QString());
			sw->setCreated(QDateTime());
			sw->setStatusId(0);
			sw->setEnabledReplyDeleteButton(false);
			sw->setUserOwnStatus(false);
			sw->setProfileImagePixmap(QPixmap());
		}
	}

	//download/load images
	if(m_imageDownloader)
		m_imageDownloader->downloadImages(urlsImages);
}

void QTwitdget::backButtonClicked()
{
	m_currentPage -= 1;

	if(m_currentPage == 0)
		ui.backButton->setEnabled(false);

	ui.forwardButton->setEnabled(true);

	updateStatusWidgets();
}

void QTwitdget::forwardButtonClicked()
{
	m_currentPage += 1;

	ui.backButton->setEnabled(true);

	if(m_statusWidgetsPerPage * (m_currentPage + 1) > m_statuses.count())
		ui.forwardButton->setEnabled(false);

	updateStatusWidgets();
}

void QTwitdget::finishedDownloadImages()
{
	QHash<QString, QImage> images = m_imageDownloader->getImages();

	for(int i = 0; i < m_statusWidgetsPerPage; ++i){
		StatusWidget *sw = m_statusWidgets.at(i);

		int atStatus = m_currentPage * m_statusWidgetsPerPage + i;
		
		if(atStatus < m_statuses.count()){
			QImage img = images.value(m_statuses.at(atStatus).profileImageUrl());
			sw->setProfileImagePixmap(QPixmap::fromImage(img));
		}
	}
}

void QTwitdget::changeEvent(QEvent *e)
{
	if(e->type() == QEvent::LanguageChange)
		ui.retranslateUi(this);

	QWidget::changeEvent(e);
}